package com.cyy.demo.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.nio.charset.Charset;
import java.util.UUID;

public class ImageRemarkUtils {

    private static Logger LOG = LoggerFactory.getLogger(ImageRemarkUtils.class);
    public static final String IMG_TEMP_FILE_PATH = System.getProperty("user.dir", "utf-8") + "/tempImg/";
    public static final String IMG_COMPOSITE_PATH = System.getProperty("user.dir", "utf-8") + "/compositeImg/";

    public static void main(String[] args) {
         LOG.debug("start......");
         LOG.debug(Charset.defaultCharset().name());
         String basePath = System.getProperty("user.dir", "utf-8");
         LOG.debug("------server root path:" + basePath);
         String path1 = basePath + "/file/pic/yejing.jpg";
         String path2 = basePath + "/file/pic/fangzi.jpg";
         String path = basePath + "/file/pic/res.jpg";
         StringBuffer sb = new StringBuffer("地点：").append("camera\\n").append(" 违法时间: ").append("2016.07.29 \\r").append(" 车牌号码: ").append("陕A88888").append(" 行驶状态: 违章停车").append(" 防伪标识码：").append(UUID.randomUUID().toString());
         String content = sb.toString();
         imageJointAndRemark(path1, path2, path, content, content);
         LOG.debug("end......");
    }

    public static String imageJointAndRemark(String img1Path, String img2Path, String resImgPath, String content1, String content2) {
        File file = new File(IMG_TEMP_FILE_PATH);
        if (!file.exists()) {
            file.mkdirs();
        }
        File file2 = new File(IMG_COMPOSITE_PATH);
        if (!file2.exists()) {
            file2.mkdirs();
        }
        LOG.debug("------target pic path=:" + resImgPath);
        try {
            File fileOne = new File(img1Path);
            BufferedImage imageFirst = ImageIO.read(fileOne);
            int[] imageArrayFirst = new int[imageFirst.getWidth() * imageFirst.getHeight()];
            imageArrayFirst = imageFirst.getRGB(0, 0, imageFirst.getWidth(), imageFirst.getHeight(), imageArrayFirst, 0, imageFirst.getWidth());

            File fileTwo = new File(img2Path);
            BufferedImage imageSecond = ImageIO.read(fileTwo);
            int[] imageArraySecond = new int[imageSecond.getWidth() * imageSecond.getHeight()];
            imageArraySecond = imageSecond.getRGB(0, 0, imageSecond.getWidth(), imageSecond.getHeight(), imageArraySecond, 0, imageSecond.getWidth());

            int width = imageFirst.getWidth() + imageSecond.getWidth();
            int height = imageFirst.getHeight() > imageSecond.getHeight() ? imageFirst.getHeight() : imageSecond.getHeight();

            int fontBgHeight = 120;
            BufferedImage imageResult = new BufferedImage(width, height + fontBgHeight, BufferedImage.TYPE_INT_RGB);

            Graphics2D g2d = imageResult.createGraphics();
            Color color = new Color(250, 5, 5, 255);
            Color color_bg = new Color(245, 245, 245, 255);
            Font font = new Font("微软雅黑", Font.BOLD, 40);
            g2d.setBackground(color_bg);
            g2d.clearRect(0, 0, width, fontBgHeight);
            g2d.setColor(color);
            g2d.setFont(font);
            g2d.drawString(content1, 20, 50);
            g2d.drawString(content2, 20, 100);
            g2d.dispose();
            imageResult.setRGB(0, 0, width, 0, new int[width * 10], 0, width);
            imageResult.setRGB(0, fontBgHeight, imageFirst.getWidth(), imageFirst.getHeight(), imageArrayFirst, 0, imageFirst.getWidth());
            imageResult.setRGB(imageFirst.getWidth(), fontBgHeight, imageSecond.getWidth(), imageSecond.getHeight(), imageArraySecond, 0,
                    imageSecond.getWidth());

            File outFile = new File(resImgPath);
            LOG.debug("new file path=" + resImgPath);
            boolean flag = ImageIO.write(imageResult, "jpg", outFile);
            return flag ? resImgPath : null;
        } catch (Exception e) {
            LOG.error("img jointing failed!", e);
        }
        return null;
    }
}
